﻿Partial Class DanoQuest

    Private prevKeyState As KeyboardState
    Private pressedKey As Keys
    Private keyRepeatTimer As Single
    Private keyRepeatStartDuration As Single = 0.3F
    Private keyRepeatDuration As Single = 0.03F

    Private Function IsKeyPressed(key As Keys, dt As Single) As Boolean
        If prevKeyState.IsKeyUp(key) Then
            keyRepeatTimer = keyRepeatStartDuration
            pressedKey = key
            Return True
        End If
        If key = pressedKey Then
            keyRepeatTimer -= dt
            If keyRepeatTimer <= 0.0F Then
                keyRepeatTimer += keyRepeatDuration
                Return True
            End If
        End If
        Return False
    End Function

    Public Sub KeyboardInput_UICommandBox(ByVal KeyboardState As KeyboardState, ByVal Amount As Single)
        Dim PressedKeys As Keys() = KeyboardState.GetPressedKeys()
        Dim Shift As Boolean = KeyboardState.IsKeyDown(Keys.LeftShift) OrElse KeyboardState.IsKeyDown(Keys.RightShift)
        For Each Key As Keys In PressedKeys
            If Not IsKeyPressed(Key, Amount) Then
                Continue For
            End If
            Dim ch As Char
            If KeyboardUtilities.KeyToString(Key, Shift, ch) Then
                GameManager.UserInterface.UICommandBox.CommandLine = GameManager.UserInterface.UICommandBox.CommandLine.Insert(GameManager.UserInterface.UICommandBox.CursorIndex, New String(ch, 1))
                GameManager.UserInterface.UICommandBox.CursorIndex += 1
            Else
                Select Case Key
                    Case Keys.Tab
                        _GameManager.SpriteManager.GetNext(GameManager.SpriteManager.Player.Target) 'Get next target from SpriteManager
                        GameManager.SpriteManager.Player.Stats.HasTarget = True
                    Case Keys.C
                        If GameManager.SpriteManager.Player.Target.IsPlayer = False And GameManager.SpriteManager.Player.Target.IsDead = False Then
                            GameManager.SpriteManager.Player.isAttacking = True
                        End If

                    Case Keys.Back
                        If GameManager.UserInterface.UICommandBox.CursorIndex > 0 Then
                            GameManager.UserInterface.UICommandBox.CommandLine = GameManager.UserInterface.UICommandBox.CommandLine.Remove(System.Threading.Interlocked.Decrement(GameManager.UserInterface.UICommandBox.CursorIndex), 1)
                        End If
                    Case Keys.Delete
                        If GameManager.UserInterface.UICommandBox.CursorIndex < GameManager.UserInterface.UICommandBox.CommandLine.Length Then
                            GameManager.UserInterface.UICommandBox.CommandLine = GameManager.UserInterface.UICommandBox.CommandLine.Remove(GameManager.UserInterface.UICommandBox.CursorIndex, 1)
                        End If
                    Case Keys.Left
                        If GameManager.UserInterface.UICommandBox.CursorIndex > 0 Then
                            GameManager.UserInterface.UICommandBox.CursorIndex -= 1
                        End If
                    Case Keys.Right
                        If GameManager.UserInterface.UICommandBox.CursorIndex < GameManager.UserInterface.UICommandBox.CommandLine.Length Then
                            GameManager.UserInterface.UICommandBox.CursorIndex += 1
                        End If
                        Exit Select
                    Case Keys.Enter
                        GameManager.UserInterface.UICommandBox.ExecuteCommand(GameManager.UserInterface.UICommandBox.CommandLine)
                        GameManager.UserInterface.UICommandBox.CommandLine = String.Empty
                        GameManager.UserInterface.UICommandBox.CursorIndex = 0
                        Exit Select
                    Case Keys.Up
                        If GameManager.UserInterface.UICommandBox.CommandHistory.Count > 0 Then
                            GameManager.UserInterface.UICommandBox.CommandHistoryIndex = Math.Max(0, GameManager.UserInterface.UICommandBox.CommandHistoryIndex - 1)

                            GameManager.UserInterface.UICommandBox.CommandLine = GameManager.UserInterface.UICommandBox.CommandHistory(GameManager.UserInterface.UICommandBox.CommandHistoryIndex)
                            GameManager.UserInterface.UICommandBox.CursorIndex = GameManager.UserInterface.UICommandBox.CommandLine.Length
                        End If
                        Exit Select
                    Case Keys.Down
                        If GameManager.UserInterface.UICommandBox.CommandHistory.Count > 0 Then
                            GameManager.UserInterface.UICommandBox.CommandHistoryIndex = Math.Min(GameManager.UserInterface.UICommandBox.CommandHistory.Count - 1, GameManager.UserInterface.UICommandBox.CommandHistoryIndex + 1)
                            GameManager.UserInterface.UICommandBox.CommandLine = GameManager.UserInterface.UICommandBox.CommandHistory(GameManager.UserInterface.UICommandBox.CommandHistoryIndex)
                            GameManager.UserInterface.UICommandBox.CursorIndex = GameManager.UserInterface.UICommandBox.CommandLine.Length
                        End If
                        Exit Select
                        'Case Else
                        '    GameManager.UICommandBox.commandLine = GameManager.UICommandBox.commandLine.Insert(GameManager.UICommandBox.cursorIndex, key.ToString)
                        '    GameManager.UICommandBox.cursorIndex += 1
                End Select
            End If
        Next
    End Sub

    Public Sub KeyboardInput_Movement(ByVal PressedKeys() As Input.Keys, ByVal Amount As Single)
        Dim MoveVector As Vector3 = Vector3.Zero
        For i = 0 To PressedKeys.Length - 1 'Enumerates thru all pressed keys
            Select Case PressedKeys(i)
                Case Keys.W
                    MoveVector += Vector3.Forward
                Case Keys.S
                    MoveVector += Vector3.Backward
                Case Keys.D
                    MoveVector += Vector3.Right
                Case Keys.A
                    MoveVector += Vector3.Left
                Case Keys.Q
                    'moveVector += Vector3.Up ToDo: Jump
                Case Keys.Z
                    'moveVector += Vector3.Down ToDo: Duck
                Case Keys.P
                    GameManager.UserInterface.UICommandBox.HasFocus = True
            End Select

            GameManager.Player.UpdatePosition(MoveVector * Amount)
        Next
    End Sub

    Public Sub KeyboardInput(ByVal Amount As Single)
        Dim KeyboardState As KeyboardState = Keyboard.GetState(PlayerIndex.One)
        Dim PressedKeys() As Input.Keys = KeyboardState.GetPressedKeys()

        If PressedKeys.Length > 0 Then
            Select Case GameManager.UserInterface.UICommandBox.HasFocus
                Case True
                    KeyboardInput_UICommandBox(KeyboardState, Amount)
                    Exit Select
                Case False
                    KeyboardInput_Movement(PressedKeys, Amount)
                    Exit Select
            End Select
        End If
    End Sub

    Public Sub MouseInput(Amount As Single)
        If (Mouse.GetState <> OriginalMouseState) Then
            Dim xDifference As Single = Mouse.GetState.X - OriginalMouseState.X
            Dim yDifference As Single = Mouse.GetState.Y - OriginalMouseState.Y
            GameManager.Camera.HorizontalRotation -= GameManager.Camera.RotationSpeed * xDifference * Amount
            GameManager.Camera.VerticalRotation -= GameManager.Camera.RotationSpeed * yDifference * Amount
            Mouse.SetPosition(CInt(Resolution.X / 2), CInt(Resolution.Y / 2))
            GameManager.Camera.UpdateViewMatrix()
        End If
    End Sub

End Class